package com.labs.laba_01;

import java.util.ArrayList;
import java.util.Arrays;

public class StudentGroup{
	public class StudRec{
		private Student Student;
		private byte number;
		private byte appraisal;
		public StudRec(Student s, byte n) {
			Student = s;
			number = n;
			appraisal = 0;
		}
		public StudRec(Student s, byte n, byte a) {
			Student = s;
			number = n;
			appraisal = a;
		}
		public Student getStudent(){
			return Student;
		}
		public byte getNumber(){
			return number;
		}
		public byte getAppraisal(){
			return appraisal;
		}
	}
	public class StudRecForSortByNumber implements Comparable<Object> {
		private StudRec str;
		private byte number = 0;
		public StudRecForSortByNumber(byte n, StudRec s){
			this.number = n;
			this.str = s;
		}
		public void setNumber(byte n){
			this.number = n;
		}
		public void setStudRec(StudRec s){
			this.str = s;
		}
		public StudRec getStudRec(){
			return str;
		}
		public int compareTo(Object obj){
			StudRecForSortByNumber tmp = (StudRecForSortByNumber)obj;
		    if(this.number < tmp.number){
		      return -1;
		    }else if(this.number > tmp.number){
		      return 1;
		    }
		    return 0;  
		}
	};
	public class StudRecForSortByAppraisal implements Comparable<Object>{
		private StudRec str;
		private byte appraisal = 0;
		public StudRecForSortByAppraisal(byte a, StudRec s){
			this.appraisal = a;
			this.str = s;
		}
		public void setAppraisal(byte a){
			this.appraisal = a;
		}
		public void setStudRec(StudRec s){
			this.str = s;
		}
		public StudRec getStudRec(){
			return str;
		}
		public int compareTo(Object obj){
			StudRecForSortByAppraisal tmp = (StudRecForSortByAppraisal)obj;
		    if(this.appraisal < tmp.appraisal){
		      return -1;
		    }else if(this.appraisal > tmp.appraisal){
		      return 1;
		    }
		    return 0;  
		}
	};
	//
	static final byte MAX_COUNT = 20;
	static final byte MAX_CAPTION_LENGTH = 15;
	private ArrayList<StudRec> studList;
	private CharSequence caption = "\0";
	private byte count = 0;
	public StudentGroup(){
		studList = new ArrayList<StudRec>();
	}
	public StudentGroup(CharSequence c){
		if(c.length()>0) caption = c;
		studList = new ArrayList<StudRec>();
	}
	public void sortByNumber(){
		StudRecForSortByNumber[] ar = new StudRecForSortByNumber[studList.size()];
    	byte i = 0;
    	for(StudRec str: studList){
    		ar[i] = new StudRecForSortByNumber(str.getNumber(), str);
    		i++;
    	}
    	Arrays.sort(ar);
    	this.studList.clear();
    	for(i=0;i<ar.length;i++){
    		this.studList.add(ar[i].getStudRec());
    	}
    	//this.studList = nStudList;
	}
	public void sortByAppraisal(){
		StudRecForSortByAppraisal[] ar = new StudRecForSortByAppraisal[studList.size()];
		byte i = 0;
		for(StudRec str: studList){
			ar[i] = new StudRecForSortByAppraisal(str.getAppraisal(), str);
			i++;
		}
		Arrays.sort(ar);
		this.studList.clear();
		for(i=0;i<ar.length;i++){
			this.studList.add(ar[i].getStudRec());
		}
    	//this.studList = nStudList;
	}

	public CharSequence getCaption(){
		return caption;
	}
	public byte getCount(){
		return count;
	}
	public ArrayList<StudRec> getList(){
		return studList;
	}
	public boolean addStudent(Student s, int a){
		if(s!=null && count<MAX_COUNT){
			StudRec sr = findStudent(s);
			if(sr!=null){
				return false;
			}else{
				sr = new StudRec(s, ++count, (byte)a);
				studList.add(sr);
				return true;
			}
		}
		return false;
	}
	public StudRec findStudent(Student s){
		if(!studList.isEmpty()){
			for(StudRec sr: studList){
				if(sr.getStudent().equals(s)){
					return sr;
				}
			}
		}
		return null;
	}
}
